package com.example.superoa.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.superoa.common.Shijian;
import com.example.superoa.model.pojos.Activity;
import com.example.superoa.model.pojos.Course;
import com.example.superoa.model.service.MenuService;
import com.example.superoa.model.service.PhotoService;
import com.example.superoa.model.service.TrainService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Controller
public class TrainController {
	@Autowired
	private TrainService ts;
	@Autowired
	private PhotoService ps;
	
	@Autowired
	private MenuService m_ser;
	
	Shijian sj=new Shijian();
	int shu;
	//新建课程
	@RequestMapping("save-course")
	@ResponseBody
	public void addcourse(Course course,int teacher_id) {
		try {
			System.err.println(course+"-"+teacher_id);
			ts.aa(course);
			ts.addcourse(teacher_id);
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println("新建课程失败");
		}
	}
	//新建培训活动
	@RequestMapping("save-activity")
	@ResponseBody
	public void addactivity(Activity act,String staffids,int course_id) {
		try {
			Date date=new Date(sj.getNetworkTime());
			StringBuffer sb = new StringBuffer(staffids);
			sb.deleteCharAt(sb.length()-1);
			sb.deleteCharAt(0);
			//分割
			List<Integer> ids = new ArrayList<>();
			String[] split = sb.toString().split(",");
			for (String s : split) {
				ids.add(Integer.parseInt(s));
				//ts.jiluadd(act, Integer.parseInt(s), course_id, ttest_id);
			}
			ts.addActone(act, course_id, date);
			ts.addActtwo(ids);
		} catch (Exception e) {
			System.err.println("新建培训活动失败");
		}
	}
	//删除课程
	@RequestMapping("deletes-course")
	@ResponseBody
	public void deletecourse(int course_id) {
		try {
			ts.deletecourse(course_id);
		} catch (Exception e) {
			System.err.println("删除课程失败");
		}
	}
	//根据讲师名称监听查询
	@RequestMapping("selectfindteacher")
	@ResponseBody
	public JSONArray findTeacherByName() {
		return ts.findTeacherByName();
	}
	//查询所有试卷
	@RequestMapping("selectfindtest")
	@ResponseBody
	public JSONArray selectfindtest() {
		return ts.findShijuan();
	}
	@RequestMapping("train-record")
	public void record() {
		JSONArray json = ts.findrecordact();
		int activity_id = 0;
		int course_id=0;
		for (Object object : json) {
			JSONObject obj=(JSONObject) object;
			activity_id=(int) obj.get("activity_id");
			course_id = (int) obj.get("course_id");
			
		}	
		if(activity_id!=0 && course_id!=0) {
			ts.jiluadd(activity_id, course_id);
		}else {
			
		}
		
	}
	//显示培训课程记录
	@RequestMapping("select-course-activity")
	@ResponseBody
	public JSONArray findCourse() {
		JSONArray i=ts.findCourse();
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhaoc", j);
			j++;
			if(obj.get("course_hour").equals(1)) {
				obj.put("course_hour", "已启用");
			}else {
				obj.put("course_hour", "已禁用");
			}
		}
		return i;
	}
	//讲师
	@RequestMapping("select-teachers")
	@ResponseBody
	public JSONArray findTeachers() {
		JSONArray i=ts.findTeachers();
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhaotw", j);
			j++;
		}
		return i;
	}
	//显示讲师申请记录
	@RequestMapping("select-apply-teacher")
	@ResponseBody
	public JSONArray findTeacher() {
		JSONArray i=ts.findTeacher();
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhaot", j);
			j++;
		}
		return i;
	}
	//显示培训课程库
	@RequestMapping("select-courseku")
	@ResponseBody
	public JSONArray findCourseku(Model model) {
		return ts.findCourseku();
	}
	//显示培训记录
	@RequestMapping("select-record")
	@ResponseBody
	public Map<String,Object> findRecord(int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i=ts.findRecord();		
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhaor", j);
			j++;
		}		
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	//删除培训记录
	@RequestMapping("delete-record")
	@ResponseBody
	public void deleteRecord(int record_id) {
		ts.deleteRecord(record_id);
	}
	//根据培训活动id查询培训详情
	@RequestMapping("actstaff")
	@ResponseBody
	public void find(int activity_id) {
		shu=activity_id;
	}	
	//根据培训活动id查询培训详情
	@RequestMapping("selectActStaff")
	@ResponseBody
	public Map<String,Object> selectActStaff(int page,int rows) {
		Map<String,Object> map = new HashMap<>();
			//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i=ts.findActStaff(shu);
		
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhao", j);
			j++;
		}
		
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	//根据课程类型查询
	@RequestMapping("selectcoursebytype")
	@ResponseBody
	public void www(Model model) {
		model.addAttribute("courseku", ts.findCoursekuBytype());
	}
	//根据课程id查询课程信息//跳到课程详情页面
	@RequestMapping("train-coursexq")
	public void aa(Model model,int courseId) {
		model.addAttribute("coursexq", ts.findCoursekuById(courseId));
		 
	}
	//课程库数据初始化
	@RequestMapping("train-bianji")
	public void sjcsh(int course_id,Model model) {
		JSONArray json = ts.sjcsh(course_id);
		System.err.println(json.get(0));
		model.addAttribute("c", json.get(0));
	}
	//删除
	@RequestMapping("sccourse")
	@ResponseBody
	public void sccourse(int activity_id) {
		ts.sccourse(activity_id);
	}
	//培训通知信息
	@RequestMapping("noticefind")
	@ResponseBody
	public Map<String,Object> noticefind(int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i=ts.noticefind();		
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhaos", j);
			j++;
		}		
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	//通知查看
	@RequestMapping("train-tz-chakan")
	public void ntchakan(int activity_id,Model model) {
		JSONArray json=ts.findntchakan(activity_id);		
		model.addAttribute("activity_id", activity_id);
		model.addAttribute("notices", json.get(0));
	}
	//培训通知表格
	@RequestMapping("findnttable")
	@ResponseBody
	public Map<String,Object> findnttable(int page,int rows,int activity_id) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i=ts.findnttable(activity_id);
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("xuhaos", j);
			j++;
		}	
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	//培训记录表格
	@RequestMapping("findrecords")
	@ResponseBody
	public Map<String,Object> findrecords(int page,int rows) {
		Map<String,Object> map = new HashMap<>();
		//先分页
		Page<Object> pg = PageHelper.startPage(page, rows);
			
		JSONArray i = ts.findrecords();
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("rxuhao", j);
			j++;
			if(obj.get("record_state").equals("1")) {
				obj.put("record_state", "通过");
			}else {
				obj.put("record_state", "不通过");
			}
		}
		//总记录
		map.put("total", pg.getTotal());
		//页内容
		map.put("rows", i);
		return map;
	}
	//删除
	@RequestMapping("delectre")
	@ResponseBody
	public void shanchure(int record_id) {
		System.err.println("删除"+record_id);
		ts.delectre(record_id);
	}
	//教师管理表格显示
	@RequestMapping("findtea")
	@ResponseBody
	public JSONArray findtea() {
		JSONArray i = ts.findtea();
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("teaxuhao", j);
			j++;
		}
		return i;
	}
	//讲师申请表格显示
	//教师管理表格显示
	@RequestMapping("findapp")
	@ResponseBody
	public JSONArray findapp() {
		JSONArray i = ts.findapp();
		int j=1;
		for (Object object : i) {
			JSONObject obj=(JSONObject) object;
			obj.put("appxuhao", j);
			j++;			
			if(obj.get("pxapproval_state").equals("1")) {
				obj.put("pxapproval_state", "未审核");
			}else if(obj.get("pxapproval_state").equals("2")) {
				obj.put("pxapproval_state", "驳回");
			}else if(obj.get("pxapproval_state").equals("3")){
				obj.put("pxapproval_state", "审核通过");
			}else {
				obj.put("pxapproval_state", "状态错误");
			}
		}
		return i;
	}
	//新增申请
	@RequestMapping("addapplys")
	@ResponseBody
	public void addapplys(String applyreason,String applytype,HttpSession session) {
		Integer id=Integer.parseInt(session.getAttribute("sid").toString());
		JSONObject json=m_ser.selectStaff(id);
		ts.addapplys(id,applyreason,applytype,json.getInteger("staff_id"));
	}
	//删除申请并判断
	@RequestMapping("delectapply")
	@ResponseBody
	public void delectApply(int apply_id) {
		ts.delectApply(apply_id);
	}
	//删除申请
	@RequestMapping("delectteas")
	@ResponseBody
	public void delectteas(int apply_id) {
			ts.delectteas(apply_id);
	}
	//删除讲师
	@RequestMapping("delecttea")
	@ResponseBody
	public void delecttea(int teacher_id) {
		ts.delecttea(teacher_id);
	}
	//删除活动课程中培训人员
	@RequestMapping("delectactstaff")
	@ResponseBody
	public void delectact(int activity_id,int staff_id) {
		ts.deletetac(activity_id,staff_id);
	}
	//图片上传
	@RequestMapping("img")
	public void imgs() {
		
	}
	@RequestMapping("myimg")
	@ResponseBody
	public String MyImage(MultipartFile fileImage) {
		ps.saveFile(fileImage);
		return "static/imgs/"+fileImage.getOriginalFilename();
	}
	@RequestMapping("applyxq")
	@ResponseBody
	public void applyxq(int apply_id,Model model) {
		JSONArray json = ts.applyxq(apply_id);	
		model.addAttribute("bou", json.get(0));
		System.err.println(json.get(0));		
	}
	@RequestMapping("train-shenqings")
	@ResponseBody
	public void tsq(String applyreason,String applytype,HttpSession session,String reason,String type,int apply_id,int ard_id) {
		Integer id=Integer.parseInt(session.getAttribute("sid").toString());
		JSONObject json=m_ser.selectStaff(id);
		ts.shenqing(id, json.getInteger("staff_id"),reason, type,apply_id,ard_id);
	}
}
